//
// (C) Copyright 2009 Irantha Suwandarathna (irantha@gmail.com)
// All rights reserved.
//

/* Copyright (c) 2001-2008, The HSQL Development Group
 * All rights reserved.
 *
 * Redistribution and use _in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions _in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer _in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the HSQL Development Group nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


using System;
using System.Text;
using EffiProzDB.Lib;
using EffiProzDB.DataTypes;

namespace EffiProzDB.RowIO
{
    /**
     * @author fredt@users
     * @since 1.7.2
     * @version 1.7.2
     */
    public class RowOutputTextLog : RowOutputBase
    {

        static  byte[] BYTES_NULL = Encoding.Default.GetBytes( "NULL");
        static  byte[] BYTES_TRUE =  Encoding.Default.GetBytes( "TRUE");
        static  byte[] BYTES_FALSE =  Encoding.Default.GetBytes( "FALSE");
        static  byte[] BYTES_AND =  Encoding.Default.GetBytes( " AND ");
        static  byte[] BYTES_IS =  Encoding.Default.GetBytes( " IS ");
        public  const int MODE_DELETE = 1;
        public  const int MODE_INSERT = 0;
        private bool isWritten;
        private int logMode;

        public void setMode(int mode)
        {
            logMode = mode;
        }

        protected override void writeFieldPrefix()
        {

            if (logMode == MODE_DELETE && isWritten)
            {
                write(BYTES_AND);
            }
        }

        protected override void writeChar(string s, int t)
        {

            write('\'');
            StringConverter.unicodeToAscii(this, s, true);
            write('\'');
        }

        protected override void writeReal(Double o, int type)
        {
            writeBytes(Column.createSQLString(o));
        }

        protected override void writeSmallint(short o)
        {
            this.writeBytes(o.ToString());
        }

        public override void writeEnd() { }

        protected override void writeTime(DateTime o)
        {

            write('\'');
            writeBytes(o.ToString());
            write('\'');
        }

        protected override void writeBinary(Binary o, int t)
        {

            ensureRoom(o.getBytesLength() * 2 + 2);
            write('\'');
            StringConverter.writeHex(getBuffer(), count, o.getBytes());

            count += o.getBytesLength() * 2;

            write('\'');
        }

        public override void writeType(int type) { }

        public override void writeSize(int size) { }

        protected override void writeDate(DateTime o)
        {

            write('\'');
            this.writeBytes(o.ToString());
            write('\'');
        }

        public override int getSize(CachedRow row)
        {
            return 0;
        }

        protected override void writeInteger(int o)
        {
            this.writeBytes(o.ToString());
        }

        protected override void writeBigint(long o)
        {
            this.writeBytes(o.ToString());
        }

        //fredt@users - patch 1108647 by nkowalcz@users (NataliaK) fix for IS NULL
        protected override void writeNull(int type)
        {

            if (logMode == MODE_DELETE)
            {
                write(BYTES_IS);
            }
            else if (isWritten)
            {
                write(',');
            }

            isWritten = true;

            write(BYTES_NULL);
        }

        protected override void writeOther(DotNetObject o)
        {

            ensureRoom(o.getBytesLength() * 2 + 2);
            write('\'');
            StringConverter.writeHex(getBuffer(), count, o.getBytes());

            count += o.getBytesLength() * 2;

            write('\'');
        }

        public override void writeString(string value)
        {
            StringConverter.unicodeToAscii(this, value, false);
        }

        protected override void writeBit(bool o)
        {
            write(o ? BYTES_TRUE
                                   : BYTES_FALSE);
        }

        protected override void writeDecimal(Decimal o)
        {
            this.writeBytes(o.ToString());
        }

        protected override void writeFieldType(int type)
        {

            if (logMode == MODE_DELETE)
            {
                write('=');
            }
            else if (isWritten)
            {
                write(',');
            }

            isWritten = true;
        }

        public override void writeLongData(long value)
        {
            this.writeBytes(value.ToString());
        }

        public override void writeIntData(int i, int position) { }

        protected override void writeTimestamp(DateTime o)
        {

            write('\'');
            this.writeLong(o.Ticks);
            write('\'');
        }

        public override void writeShortData(short i)
        {
            writeBytes(i.ToString());
        }

        public override void writeIntData(int i)
        {
            writeBytes(i.ToString());
        }

        public override void reset()
        {

            base.reset();

            isWritten = false;
        }
    }
}
